// pages/orders/detail/detail.js
import http from '../../../services/http/index'
import { PlainData } from '../../../utils/Data'


// 公共详情 - 公用【‘机场接送’，‘预约用车’】
var succeCal = function (data, _this) {
  var markers = _this.data.markers;
  var polyline = _this.data.polyline;
  var map = false;
  var MissionDetailData = Object.assign(new PlainData(), {
    data: data
  })
  // 出发地
  var MissionDetailStartData = MissionDetailData.data.info.departure;
  // 目的地
  var MissionDetailDestinationData = MissionDetailData.data.info.destination;
  // ordersType-0:班车
  // ordersType-1:接机单
  // ordersType-2:预约用车
  // ordersType-3:包车
  if (_this.data.ordersType == 1 || _this.data.ordersType == 2) {
    //接机单 预约用车
    if (data.info.routes.wayPoints.length > 0) {
      markers[0].latitude = Number(data.info.routes.wayPoints[0].latitude);
      markers[1].latitude = Number(data.info.routes.wayPoints[1].latitude);
      markers[0].longitude = Number(data.info.routes.wayPoints[0].longitude);
      markers[1].longitude = Number(data.info.routes.wayPoints[1].longitude);
      data.info.routes.waps.forEach(function (el, i) {
        el.latitude = Number(el.latitude);
        el.longitude = Number(el.longitude)
      })
      polyline[0].points = data.info.routes.waps
      map = true;
      wx.createMapContext('map').includePoints({
        padding:[10],
        points:polyline[0].points
      })
    };
  }
  if (_this.data.ordersType == 0) {
    //班车
    markers[0].latitude = Number(data.bording[0].lat);
    markers[0].longitude = Number(data.bording[0].lng);
    markers[1].latitude = Number(data.get_off[0].lat);
    markers[1].longitude = Number(data.get_off[0].lng);
    polyline[0].points = [
      {
        latitude: Number(data.bording[0].lat),
        longitude: Number(data.bording[0].lng)
      },
      {
        latitude: Number(data.get_off[0].lat),
        longitude: Number(data.get_off[0].lng)
      }
    ]
    map = true;
    wx.createMapContext('map').includePoints({
      padding:[10],
      points:polyline[0].points
    })
  }

  _this.setData({
    MissionDetail: MissionDetailData.data,
    MissionDetailStart: MissionDetailStartData,
    MissionDetailDestination: MissionDetailDestinationData,
    // latitude: MissionDetailData.data.info.routes.wayPoints[0].latitude,
    // longitude: MissionDetailData.data.info.routes.wayPoints[0].longitude,
    'markers': markers,
    'polyline': polyline,
    'map': map
  });

};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordersType:0,
    map: false,
    ordersId:'',//当前订单的id
    longitude: '', //出发地经度------公用【‘机场接送’，‘预约用车’】
    latitude: '',  //出发地纬度------公用【‘机场接送’，‘预约用车’】
    markers: [{
      latitude: 23.21229,
      longitude: 113.324520,
      width: 50,
      height: 40,
      callout: {
        content: '出发地',
        color: '#fff',
        bgColor: '#F1604F',
        display: 'ALWAYS',
        padding: 20,
        borderRadius: 40
      },
    },
    {
      latitude: 23.18229,
      longitude: 113.3245211,
      width: 50,
      height: 40,
      callout: {
        content: '目的地',
        color: '#fff',
        bgColor: '#F1604F',
        display: 'ALWAYS',
        padding: 20,
        borderRadius: 40
      },
      }],// 出发点------公用【‘机场接送’，‘预约用车’】
    polyline: [{
      points: [],
      color: "#FF0000DD",
      width: 4,
      dottedLine: true,
    }],// 路线------公用【‘机场接送’，‘预约用车’】
    MissionDetailStart: [],  //出发地------公用【‘机场接送’，‘预约用车’】
    MissionDetailDestination: [], //目的地------公用【‘机场接送’，‘预约用车’】
    MissionDetail: {},   //详情页的信息------公用【‘机场接送’，‘预约用车’】

    charterDetail:{},//包车详情页信息
    charterDetail_renshu: '',//包车详情页-乘客数
    charterDetail_schedule: '',//包车详情页-行程
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      ordersType:options.type,
      id: options.orderId,
    })
    // ordersType-0:班车
    // ordersType-1:接机单
    // ordersType-2:预约用车
    // ordersType-3:包车
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title:'订单详情'
    })
    if (this.data.ordersType == 0) {
      // 订单详情-班车
      this.loadGetBusDetail();
    }
    if (this.data.ordersType == 1) {
      // 订单详情-机场接送
      this.loadMissionDetail();
    } else if (this.data.ordersType == 2) {
      // 订单详情-预约用车
      this.loadAppointCarDetail();
    } else if (this.data.ordersType == 3) {
      // 订单详情-包车
      this.loadCharterDetail();
    }

  },


  // 请求-订单详情-接送机
  loadMissionDetail() {   
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    http.getMissionDetail({id:this.data.id})
      .then(data => {
        succeCal(data,this);
        console.log('接送机订单详情-')
        console.log(data)
        wx.hideToast();
      }).catch(function (err) {
        wx.hideToast();
      })
  },
  // 请求-订单详情-预约用车
  loadAppointCarDetail() {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    http.getAppointCarDetail({ id: this.data.id })
      .then(data => {
        succeCal(data, this);
        console.log('预约用车订单详情')
        console.log(data)
        wx.hideToast();
      }).catch(function (err) {
        wx.hideToast();
      })
  },
  // 请求-订单详情-班车
  loadGetBusDetail() {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    http.getBusDetail({ id: this.data.id })
      .then(data => {
        succeCal(data, this);
        console.log('班车订单详情')
        console.log(data)
        wx.hideToast();
      }).catch(function (err) {
        wx.hideToast();
      })
  },

  // 请求-订单详情-包车
  loadCharterDetail() {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    http.getCharterDetail({ id: this.data.id })
      .then(data => {
        // succeCal(data, this);
        console.log('包车订单详情')
        console.log(data)
        wx.hideToast();
        var charterDetailData = Object.assign(new PlainData(), {
          data: data
        })
        console.log(data.info.adults)
        var charterDetail_rennum = data.info.adults + data.info.children
        var charterDetail_scheduleData = data.schedule;
        this.setData({
          charterDetail: charterDetailData.data,
          charterDetail_renshu: charterDetail_rennum,
          charterDetail_schedule: charterDetail_scheduleData
        });
      }).catch(function (err) {
        wx.hideToast();
      })
  },


  





  // 抢单成功
  robbingSucces() {
    wx.showToast({
      title: '抢单成功',
      icon: 'succes',
      duration: 5000,
      success: function () {
        setTimeout(function () {
          //要延时执行的代码
          wx.switchTab({
            url: '../../orderManage/orderManage'
          })
        }, 2000) //延迟时间
      }
    })
  },
// 抢单失败
  robbingFai(){
    wx.showModal({
      title: '',
      content: err.message,
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }
      }
    })
  },


  // 请求-抢单 接机单
  RobbingMission(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    var that = this;
    var idRobbing = e.currentTarget.dataset.id;
    console.log("idRobbing是" + idRobbing)
    http.getRobbingMission({ id: idRobbing }).then(data => {
      console.log(data)
      // 抢单成功
      that.robbingSucces();
      wx.hideToast();
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
      if (err.payload.status == 2) {
        wx.showModal({
          title: '',
          content: err.message,
          success: function (res) {
            if (res.confirm) {
              http.getRobbingMission({ id: idRobbing, is_confirm: 1 }).then(data => {
                // 抢单成功
                that.robbingSucces();
              }).catch(function (err) {
              })
            } else {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        // "预约用车"无法抢单
        that.robbingFai();
      }
    })
  },

  // 请求-抢单 预约用车
  CarMission(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    var that = this;
    var idRobbing = e.currentTarget.dataset.id;
    console.log("idRobbing是" + idRobbing)
    http.appointCarGrap({ id: idRobbing }).then(data => {
      console.log(data)
      // "预约用车"抢单成功
      that.robbingSucces();
      wx.hideToast();
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
      if (err.payload.status == 2) {
        wx.showModal({
          title: '',
          content: err.message,
          success: function (res) {
            if (res.confirm) {
              http.appointCarGrap({ id: idRobbing, is_confirm: 1 }).then(data => {
                // "预约用车"抢单成功
                that.robbingSucces();
              }).catch(function (err) {

              })
            } else {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        // "预约用车"无法抢单
        that.robbingFai();
      }
    })
  },


  //复制
  copyAddress(e) {
    var self = this;
    var index = e.currentTarget.dataset['index'];
    var type = e.currentTarget.dataset['type'];
    wx.setClipboardData({
      data: this.data.MissionDetail.info[type][index],
      success: function (res) {
        // self.setData({copyTip:true}),
        wx.showModal({
          title: '提示',
          content: '复制成功',
          success: function (res) {
            if (res.confirm) {
              console.log('确定')
            } else if (res.cancel) {
              console.log('取消')
            }
          }
        })
      }
    });
  },
  copyAddress_bao(e) {
    var self = this;
    wx.setClipboardData({
      data: this.data.charterDetail.info.boarding_place,
      success: function (res) {
        // self.setData({copyTip:true}),
        wx.showModal({
          title: '提示',
          content: '复制成功',
          success: function (res) {
            if (res.confirm) {
              console.log('确定')
            } else if (res.cancel) {
              console.log('取消')
            }
          }
        })
      }
    });
  },

  //打开导航
  goLocation(e){
    var typeindex = e.currentTarget.dataset['typeindex'];
    var type = e.currentTarget.dataset['type'];
    var ordersType = this.data.ordersType;
    var gps = {
      latitude:'',
      longitude:''
    };
    if(ordersType == 1 || ordersType == 2){
      //接机单和预约用车
      gps = this.data.MissionDetail.info.routes.wayPoints[typeindex]
    }

    if (ordersType == 0){
      //班车
      gps = {
        latitude:this.data.MissionDetail[type][typeindex].lat,
        longitude:this.data.MissionDetail[type][typeindex].lng
      }
    };

    console.log(gps);
    wx.openLocation({
      latitude: Number(gps.latitude),
      longitude: Number(gps.longitude),
      scale: 28
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})